from sanic import Sanic
from sanic.response import json
# chatgpt的方法
from utils.auth_uitl import sync_vivogpt
# 向量搜索的方法（比较相似度最大）
from utils.auth_uitl import get_similarity
from utils.auth_uitl import  embedding

app = Sanic("MyApp")
@app.route('/')
async def test(request):
    return json({"hello": "world"})

# 一问一答的chatgpt
@app.post('/chatgpt')
async def chatgpt(request):
    # 获取request的text文本内容
    text=request.json['message']
    username=request.json['username']
    response=sync_vivogpt(text)
    return json({"statusCode":"200","message": response,"username":username})
# 将相似度向量数据库进行更新
@app.post('/update/similarity')
async def updateSimilar(request):
    text=request.json['message']
    username=request.json['username']
    rep=embedding(text)
    return json({"statusCode":"200","message": rep,"username":username})
# 根据用户需求给用户top3的相似度运动推荐
@app.post('/get/similarity')
async def getSimilarity(request):
    text=request.json['message']
    username=request.json['username']
    max_similar=get_similarity([text])
    return json({"statusCode":"200","message": max_similar.tolist(),"username":username})


if __name__ == '__main__':
    app.run(host="127.0.0.1", port=5000)
